package com.school.graduation.ui.activity.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.school.graduation.R;
import com.school.graduation.bean.SignCallBack;
import com.school.graduation.classification.view.ClassIficationFragment;
import com.school.graduation.goods.view.GoodsFragment;
import com.school.graduation.internet.SignInterface;
import com.school.graduation.mine.view.MineFragment;
import com.school.graduation.ui.activity.mainUi.ImplMainActivity;
import com.school.graduation.util.ToastUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static cn.bmob.v3.Bmob.getApplicationContext;
import static com.school.graduation.view.SignInActivity.SIGN_IN_SUCCESS;

/**
 * Created by U-nookia on 2017/3/10.
 */

public class MainActivityPresenter {

    private Context context;
    private ImplMainActivity mainActivity;

    private GoodsFragment goods = null;
    private MineFragment mine = null;
    private ClassIficationFragment ification = null;

    private FragmentManager manager;

    public MainActivityPresenter(Context context,ImplMainActivity mainActivity) {
        this.context = context;
        this.mainActivity = mainActivity;
    }

    public void setHomeFragment() {
        if (goods==null) goods = new GoodsFragment();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.mainFrame, goods);
        transaction.commit();
    }

    public void setFragmentManager() {
        manager = mainActivity.getMainFragmentManager();
    }

    public void setClassificationFragment() {
        if (ification==null) ification = new ClassIficationFragment();
        FragmentTransaction transaction1 = manager.beginTransaction();
        transaction1.replace(R.id.mainFrame,ification);
        transaction1.commit();
    }

    public void setMineFragment() {
        if (mine==null) mine = new MineFragment();
        FragmentTransaction transaction1 = manager.beginTransaction();
        transaction1.replace(R.id.mainFrame,mine);
        transaction1.commit();
    }

    public void login() {
        SharedPreferences sp = getApplicationContext().getSharedPreferences("user_cache", Context.MODE_MULTI_PROCESS |
                Activity.MODE_WORLD_READABLE);
        String name = sp.getString("name","no_name_flag");
        String password = sp.getString("password","no_password_flag");
        signIn(name,password);
    }

    private void signIn(String name, String password) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://123.206.20.29:3000/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SignInterface sign = retrofit.create(SignInterface.class);
        Call<SignCallBack> call = sign.signIn(name,password);
        call.enqueue(new Callback<SignCallBack>() {
            @Override
            public void onResponse(Call<SignCallBack> call, Response<SignCallBack> response) {
                if (response.isSuccessful()){
                    SignCallBack callBack = response.body();
                    signIn(callBack);
                }else ToastUtil.makeShortToast(context,response.errorBody().toString());
            }

            @Override
            public void onFailure(Call<SignCallBack> call, Throwable t) {
                ToastUtil.makeShortToast(context,t.toString());
            }
        });
    }

    private void signIn(SignCallBack callBack) {
        int user = callBack.getUser();
        if (user==SIGN_IN_SUCCESS){
            //ToastUtil.makeShortToast(context,callBack.getStatus());
        }else {
            ToastUtil.makeShortToast(context,callBack.getStatus());
        }
    }
}
